package com.example.forum_system.searchEngine.titleList;

import lombok.extern.slf4j.Slf4j;

/**
 * @BelongsProject: 0.forum_system
 * @BelongsPackage: com.example.forum_system.SE
 * @Author : 游在海里的鱼
 * @CreateTime : 2023-11-26  22:37
 * @Version : 1.0
 * @Description : 标题列表（暂未使用，跟 WordIndexList同质）
 */
@Slf4j
public class TitleList {
    //默认容量
    private static final int CAPACITY_DEFAULT = 10;
    //最小扩容数
    private static final int DILATATION_MIN = 5;
    //最大扩容数
    private static final int DILATATION_MAX = 100000;

    private int[] titleList = new int[CAPACITY_DEFAULT];


    public boolean add(int pos,int end){
        if(pos>= titleList.length){
            dilatation();
        }
        titleList[pos] = end;
        return true;
    }

    public int get(int pos){
        if(pos>=titleList.length || pos<0){
            log.error("索引超出数组长度");
            throw new IndexOutOfBoundsException();
        }
        return titleList[pos];
    }

    public int set(int pos,int end){
        if(pos>=titleList.length || pos<0){
            log.error("索引超出数组长度");
            throw new IndexOutOfBoundsException();
        }
        titleList[pos] = end;
        return end;
    }

    public int remove(int pos){
        if(pos>=titleList.length || pos<0){
            log.error("索引超出数组长度");
            throw new IndexOutOfBoundsException();
        }
        int ret = titleList[pos];
        titleList[pos] = -1;
        return ret;
    }

    /*------------------------------------普--通--方--法------------------------------------------------------------------
     * @Desc  : 扩容
     * @Param : []
     * @Return: void
     **/
    private void dilatation(){
        //1.得到合理的扩容长度
        //源容量的1.5倍
        int newlen = (int)(1.5*titleList.length);
        //小于最小扩容量，比如 （int）1*1.5 = 1
        if(newlen <= titleList.length + DILATATION_MIN){
            newlen = titleList.length + DILATATION_MIN;
        }
        //大于最大扩容量，扩容太多了也没必要
        if(newlen - titleList.length > DILATATION_MAX){
            newlen = titleList.length + DILATATION_MAX;
        }
        int[] newarr = new int[newlen];
        System.arraycopy(titleList,0,newarr,0,titleList.length);
        titleList = newarr;
    }
}
